//
//  YWNewTroubleMutableBIllCell.m
//  maintain
//
//  Created by 郑想 on 16/7/25.
//  Copybottom © 2016年 ewin. All bottoms reserved.
//

#import "YWTroubleMutableBIllCell.h"
#import "YWHandler.h"
#import "YWCirculationInfo.h"
#import "YWCommonTool.h"
#import "UIImageView+WebCache.h"
#import "UIColor+CustomColors.h"
#import "YWMission.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"

#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"
#import "YYLabel.h"

@interface YWTroubleMutableBIllCell()

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

/** 说明 */
@property (nonatomic, strong) YYLabel *noteLabel;
/** 时间 */
@property (nonatomic, strong) YYLabel *timeLabel;
/** 头像 */
@property (nonatomic, strong) UIImageView *headImageView;

@end

@implementation YWTroubleMutableBIllCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWTroubleMutableBIllCell *cell = [tableView dequeueReusableCellWithIdentifier:YWTroubleMutableBIllCellIdentifier];
    if (cell == nil) {
        cell = [[YWTroubleMutableBIllCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWTroubleMutableBIllCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
        self.clipsToBounds = YES;
    }
    return self;
}

-(void)setIsFirstRow:(BOOL)isFirstRow{
    _isFirstRow = isFirstRow;
}


- (void)setTroubleMissionModel:(YWTroubleMissionModel *)troubleMissionModel{
    _troubleMissionModel = troubleMissionModel;
}

-(void)setCirculationInfo:(YWCirculationInfo *)circulationInfo{
    _circulationInfo = circulationInfo;
    YWHandler *creator = _circulationInfo.author;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:creator.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    NSString* timeStr = nil;
    if (_circulationInfo.createTime) {
        timeStr = [YWCommonTool showTimeWithNormalFormatter:_circulationInfo.createTime / 1000 ];
    }else{
        timeStr = [YWCommonTool showTimeWithNormalFormatter:[[NSDate date] timeIntervalSince1970]];
    }
    _timeLabel.text = timeStr?:@"";
    
    if (_circulationInfo.type == YWCirculationInfoTypePostTrouble){
        _noteLabel.text = [NSString stringWithFormat:@"%@报障",_circulationInfo.author.userName?:@""];
    }else if (_circulationInfo.type == YWCirculationInfoTypeCancelTrouble){
        _noteLabel.text = [NSString stringWithFormat:@"%@撤回报障 备注: %@",_circulationInfo.author.userName?:@"",_circulationInfo.note.length>0?_circulationInfo.note:@"无"];
    }else if (_circulationInfo.type == YWCirculationInfoTypeAssignTroubleMission){
        NSString *cutOffTime = @"";
        if (_circulationInfo.isLastAssignBill && _troubleMissionModel) {
            cutOffTime = [NSString stringWithFormat:@" 请于%@前完成维修",[YWCommonTool showDate:_troubleMissionModel.cutoffTime/1000]];
        }
        _noteLabel.text = [NSString stringWithFormat:@"%@分配给%@ 备注:%@%@",_circulationInfo.author.userName?:@"",_circulationInfo.recipient.userName?:@"",_circulationInfo.note.length>0?_circulationInfo.note:@"无",cutOffTime];
    }else if (_circulationInfo.type == YWCirculationInfoTypeCancelTroubleMission){
        _noteLabel.text = [NSString stringWithFormat:@"%@撤回维修任务",_circulationInfo.author.userName?:@""];
    }else if (_circulationInfo.type == YWCirculationInfoTypeReturnTroubleMission){
        _noteLabel.text = [NSString stringWithFormat:@"%@退回维修任务 备注: %@",_circulationInfo.author.userName?:@"",_circulationInfo.note.length>0?_circulationInfo.note:@"无"];
    }else if (_circulationInfo.type == YWCirculationInfoTypeFinishTroubleMission){
        _noteLabel.text = [NSString stringWithFormat:@"%@完成维修任务",_circulationInfo.author.userName?:@""];
    }else{
        _noteLabel.text = @"无";
    }
    _noteLabel.textColor = _isFirstRow?[UIColor customBlueColor]:[UIColor blackColor];
    
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
}


- (void) setUpSubviews{
    UIView *superview = self.contentView;
    /**
     *  |
     * 头像
     *  |
     */
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superview).offset(8);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview);
        make.bottom.mas_equalTo(_headImageView.mas_top);
    }];
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImageView.mas_bottom);
        make.bottom.mas_equalTo(superview);
        make.centerX.mas_equalTo(@[_headImageView, _topLine]);
        make.width.mas_equalTo(@[@1, _topLine]);
    }];
    /**
     *  说明
     *  时间
     */
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview).offset(8);
        make.top.mas_equalTo(_headImageView);
        make.left.mas_equalTo(_headImageView.mas_right).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
        make.bottom.mas_equalTo(superview).offset(-8);
    }];
}

- (UIView *)topLine{
    if (!_topLine) {
        _topLine = [UIView new];
        _topLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_topLine];
        _topLine.tag = KTopLineTag;
    }
    return _topLine;
}

- (UIImageView *)headImageView{
    if (!_headImageView) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 17.0;
        _headImageView = imageView;
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [UIView new];
        _bottomLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_bottomLine];
        _bottomLine.tag = KBottomLineTag;
    }
    return _bottomLine;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

@end
